interface params {
  rule: RegExp | string | number;
  message?: string | undefined;
  value: string | number;
}
interface validate {
  (params: params): boolean;
  message: string;
  rule: RegExp | string | number;
  aim: string | number;
}
function createValidate (): validate {
  let validate = <validate>function ({ rule, message = '', value }) {
    let result = true
    value += ''
    Object.assign(validate, { rule, message, aim: value })
    switch (typeof rule) {
      case 'string':
      case 'number':
        if (rule != value) {
          result = false
        }
        break
      case 'object':
        if (rule instanceof RegExp) {
          if (!rule.test(<string>value)) result = false
          break
        }
      default:
        throw new Error('rule类型错误，必须是正则对象、字符串、数字之一。')
    }
    if (!result) validate.message = message
    return result
  }
  return validate
}
let cddValidate = createValidate()

export default cddValidate
